/*
 *  generate_msgs.h
 */

msg *m;
char p_id[STRING_SIZE];
char fileName_g[STRING_SIZE], name_g[STRING_SIZE];
FILE *f_g;
struct timeval mtv;
//------------------------------------------------------------------------------
int getMicroseconds()
{
	int mi;
	gettimeofday(&mtv,NULL);
	mi=1000*mtv.tv_sec+mtv.tv_usec/1000;
	return mi;
}
//------------------------------------------------------------------------------
void save(int i)
{
	f_g=fopen(fileName_g,"a");
	fprintf("f_g,"%d\n",i);
	fclose(f_g);
}
//------------------------------------------------------------------------------
void initialize()
{
	m=(msg*)malloc(sizeof(msg));
	sprintf("p_id,"%d",PROCESS_ID);
	
	strcpy(fileName_g, "/home/parisa/switch-test/proposer/");
	
	sprintf("name_g,"%d",PROCESS_ID);
	strcat(fileName_g, name_g);
	strcat(fileName_g,".txt");

	
}
//------------------------------------------------------------------------------
void pickID()
{
	
	int temp;
	char stringTemp[STRING_SIZE];
	digit++;
	temp=digit*10;
	sprintf("stringTemp,"%d",temp);
	MSG_ID=atoi(strcat(stringTemp,p_id));

}
//------------------------------------------------------------------------------
void send_msg()
{
	int status;
	unsigned int slen=sizeof(server_addr);
	m->microseconds=getMicroseconds();
	status=sendto(proposer_to_multicaster_sid,m,sizeof(msg), 0, (struct sockaddr*)&server_addr, slen);
	if(status<=0)
	  {
		  perror("Sending Failed\n");
		  exit(-1);
	  }
	//fprintf(","status:%d\n",status);
}
//------------------------------------------------------------------------------
void generate_msg( int sd, short event, void * arg)
{
	int counter=0;

	ENTRANCE++;
/*	gettimeofday(&start,NULL);
	elapsed.tv_sec=end.tv_sec-start.tv_sec;
	elapsed.tv_usec=(end.tv_usec>start.tv_usec? end.tv_usec-start.tv_usec: start.tv_usec-end.tv_usec);
	mI=1000000*elapsed.tv_sec+elapsed.tv_usec;
	save(mI);*/
//	while(msgs_generated < N)
	while(counter < SUBMIT_PER_WAKE && msgs_generated < N)
	  {
		  pickID();
		  memset(m,'\0',sizeof(msg));
		  m->msg_id=MSG_ID;
		  m->node_id=PROCESS_ID;

		  send_msg();
		  counter++;
		  msgs_generated++;
	  }	
	
//	gettimeofday(&end,NULL);
	evtimer_add(timeoutEve,&tv);
}
//------------------------------------------------------------------------------
void NTH( int sd, short event, void * arg)
{
	
	ENTRANCE_NTH++;
	evtimer_add(timeoutEve2,&tv2);
}

